/**
 * Labox有限公司项目InnovationServer
 * Labox版权所有 @copyright 2010
 * @javaclass AnimationModelSvn.java @author 丁宁 @time 2010-7-22
 */
package labox.innovation.loginserver.svn;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.config.Config;
import labox.innovation.loginserver.svn.Svn.Data;
import labox.innovation.loginserver.svn.Svn.Resouce;

/**
 * @author Administrator
 * 
 */
public final class AnimationImageSvn implements ResouceInterface {
	
	protected static final Logger _log = LoggerFactory.getLogger(AnimationImageSvn.class.getName());

	public final Resouce load() {
		File f = new File(Config.DATAPACK_ROOT, "/Object/animationimage.pak");
		if (f.exists() && f.isFile()) {
			DataInputStream fis = null;
			try {
				fis = new DataInputStream(new FileInputStream(f));
			} catch (FileNotFoundException e) {
				_log.error("Failed to load animation file (" + f.getName() + ") for reading. Reason: " + e.getMessage(), Config.SERVER_ID, e);
				return null;
			}

			try {
				int res_svn = fis.readInt();
				int size = fis.readShort();
				ArrayList<Data> _data = new ArrayList<Data>();
				for (int i = -1; ++i < size;) {
					int svn = fis.readInt();
					byte[] data = new byte[fis.readInt()];
					fis.read(data, 0, data.length);
					_data.add(new Data(svn, data));
				}
				return new Resouce(res_svn, _data);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (fis != null)
					try {
						fis.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}

		} else {
			_log.info("Animation Image file (" + f.getName() + ") is missing or is a directory, skipped.");
		}
		return null;
	}

}
